package org.mlz.game.model

import com.sun.javafx.scene.traversal.Direction
import org.itheima.kotlin.game.core.Painter
import org.mlz.game.Config
import org.mlz.game.business.Attackable
import org.mlz.game.business.AutoMovable
import org.mlz.game.business.Destroyable
import org.mlz.game.business.Sufferable
import org.mlz.game.ext.checkCollision


/**
 *<p>
 *子弹
 * create()函数返回两个值,是x,y 高级的编程方式
 *</p>
 * CreateBy  Malingzhao
 * @date  2020/9/18
 */
class Bullet(override val owner: View,override val currentDirection: Direction, create: (width: Int, height: Int) -> Pair<Int, Int>) : View,
    AutoMovable, Destroyable, Attackable,Sufferable {

    //给子弹一个方向 方向由坦克来决定
    override val width: Int

    override val height: Int

    override val speed: Int = 8




    /**
     * 攻击力
     */
    override val attackPower: Int = 1

    //判断子弹是否被销毁了
    private var isDestroyed = false

    /**
     * 生命值
     */
    override val blood: Int = 1



    override var x: Int = 0
    override var y: Int = 0
    override fun autoMove() {
        //根据自己的方向来改变自己的x 和y
        when (currentDirection) {
            Direction.UP -> y -= speed
            Direction.DOWN -> y += speed
            Direction.LEFT -> x -= speed
            Direction.RIGHT -> x += speed
        }
    }


    val imgPath = when (currentDirection) {
        Direction.UP -> "img/shot_top.gif"
        Direction.DOWN -> "img/shot_bottom.gif"
        Direction.LEFT -> "img/shot_left.gif"
        Direction.RIGHT -> "img/shot_right.gif"
        Direction.NEXT -> TODO()
        Direction.NEXT_IN_LINE -> TODO()
        Direction.PREVIOUS -> TODO()
    }

    init {

        val size = Painter.size(imgPath)
        width = size[0]
        height = size[1]
        //先计算宽度和高度
        val pair = create.invoke(width, height)
        x = pair.first
        y = pair.second
    }


    override fun draw() {
        var imgPath = when (currentDirection) {
            Direction.UP -> "img/shot_top.gif"
            Direction.DOWN -> "img/shot_bottom.gif"
            Direction.LEFT -> "img/shot_left.gif"
            Direction.RIGHT -> "img/shot_right.gif"
            Direction.NEXT -> TODO()
            Direction.NEXT_IN_LINE -> TODO()
            Direction.PREVIOUS -> TODO()
        }
        Painter.drawImage(imgPath, x, y)
    }

    /**
     * 判断是否销毁了
     */
    override fun isDestroyed(): Boolean {
        if (isDestroyed){
            return true
        }
        //子弹到了屏幕之后进行销毁
        if (x < -width) {
            return true
        }
        if (x > Config.gameWidth) {
            return true
        }
        if (y < -height) {
            return true
        }
        if (y > Config.gameHeight) {
            return true
        }
        return false
    }

    /**
     * 死给你看的功能 ? 可以为空
     */
    override fun showDestroy(): Array<View>? {
        return null
    }


    override fun isCollission(sufferable: Sufferable): Boolean {
        return checkCollision(sufferable)
    }

    override fun notifyAttack(sufferable: Sufferable) {
//        println("子弹接收到碰撞了.....")
        isDestroyed = true
    }

    override fun notifySuffer(attackable: Attackable): Array<View>? {

        return arrayOf(Blast(x,y))
    }

}
